package com.secondhand.project.controller;

import com.secondhand.common.annotation.Log;
import com.secondhand.common.core.controller.BaseController;
import com.secondhand.common.core.domain.AjaxResult;
import com.secondhand.common.core.page.TableDataInfo;
import com.secondhand.common.enums.BusinessType;
import com.secondhand.common.utils.poi.ExcelUtil;
import com.secondhand.project.domain.ChatInfo;
import com.secondhand.project.service.IChatInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/project/info")
public class ChatInfoController extends BaseController {
    @Autowired
    private IChatInfoService chatInfoService;

    /**
     * 查询聊天记录
     */
    @GetMapping("/selectUserChat/{chatUserId}")
    public AjaxResult selectUserChat(@PathVariable Long chatUserId) {
        List<ChatInfo> chatInfoList = chatInfoService.selectUserChat(chatUserId);
        return success(chatInfoList);
    }

    @PutMapping("/updateRead/{chatUserId}")
    public AjaxResult updateRead(@PathVariable Long chatUserId) {
        chatInfoService.updateRead(chatUserId);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('project:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatInfo chatInfo) {
        startPage();
        List<ChatInfo> list = chatInfoService.selectChatInfoList(chatInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:info:export')")
    @Log(title = "聊天信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatInfo chatInfo) {
        List<ChatInfo> list = chatInfoService.selectChatInfoList(chatInfo);
        ExcelUtil<ChatInfo> util = new ExcelUtil<ChatInfo>(ChatInfo.class);
        util.exportExcel(response, list, "聊天信息数据");
    }

    @PreAuthorize("@ss.hasPermi('project:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(chatInfoService.selectChatInfoById(id));
    }

    @PreAuthorize("@ss.hasPermi('project:info:add')")
    @Log(title = "聊天信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatInfo chatInfo) {
        return toAjax(chatInfoService.insertChatInfo(chatInfo));
    }

    @PreAuthorize("@ss.hasPermi('project:info:edit')")
    @Log(title = "聊天信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatInfo chatInfo) {
        return toAjax(chatInfoService.updateChatInfo(chatInfo));
    }

    @PreAuthorize("@ss.hasPermi('project:info:remove')")
    @Log(title = "聊天信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(chatInfoService.deleteChatInfoByIds(ids));
    }
}
